home *** CD-ROM | disk | FTP | other *** search
- /* $Id: COMCamC.cpp 1.4 1997/04/11 02:32:09 damien Exp $ */
-
- // Copyright (c) 1995, Ray Dream, Inc. All rights reserved.
-
- ////////////////////////////////////////////////////////////////////////
- // Second Camera Example : Conical Camera //
- //--------------------------------------------------------------------//
- // Implementation of the Conical Camera Interface //
- ////////////////////////////////////////////////////////////////////////
-
- #include "math.h"
-
- #ifndef __COMCAMC__
- #include "COMCAMC.h"
- #endif
-
- #ifndef __CAMCDLL__
- #include "CAMCDLL.h"
- #endif
-
- #ifndef __3DCOFAIL__
- #include "3DCoFail.h"
- #endif
-
- NUM3D kQuickFocal = 50.0; // Focal = 50 mm
- NUM3D kQuickmmTo3DUnit = 10.0/1016.0; // Conversion mm to 3D Unit
- NUM3D kQuick288 = 288.0; // Conversion 3D Unit to Pt Unit
-
- // Tool to get the Global Coordinates from the Local Coordinates
- void LocalToGlobal(TRANSFORM3D* transform,VECTOR3D* LocalPos,VECTOR3D* GlobalPos) {
- // Axes rotations
- (*GlobalPos)[0]=transform->fR.fix*(*LocalPos)[0]
- +transform->fR.fjx*(*LocalPos)[1]
- +transform->fR.fkx*(*LocalPos)[2];
- (*GlobalPos)[1]=transform->fR.fiy*(*LocalPos)[0]
- +transform->fR.fjy*(*LocalPos)[1]
- +transform->fR.fky*(*LocalPos)[2];
- (*GlobalPos)[2]=transform->fR.fiz*(*LocalPos)[0]
- +transform->fR.fjz*(*LocalPos)[1]
- +transform->fR.fkz*(*LocalPos)[2];
- // Unit conversion : Points Units to 3D Units
- (*GlobalPos)[0] /= 288.0;
- (*GlobalPos)[1] /= 288.0;
- (*GlobalPos)[2] /= 288.0;
- // Origin translation
- (*GlobalPos)[0] += transform->fT[0];
- (*GlobalPos)[1] += transform->fT[1];
- (*GlobalPos)[2] += transform->fT[2];
- }
-
- // Tool to get the Global Coordinates from the Local Coordinates for a Vector
- void LocalToGlobalVector(TRANSFORM3D* transform,VECTOR3D* LocalPos,VECTOR3D* GlobalPos) {
- // Axes rotations
- (*GlobalPos)[0]=transform->fR.fix*(*LocalPos)[0]
- +transform->fR.fjx*(*LocalPos)[1]
- +transform->fR.fkx*(*LocalPos)[2];
- (*GlobalPos)[1]=transform->fR.fiy*(*LocalPos)[0]
- +transform->fR.fjy*(*LocalPos)[1]
- +transform->fR.fky*(*LocalPos)[2];
- (*GlobalPos)[2]=transform->fR.fiz*(*LocalPos)[0]
- +transform->fR.fjz*(*LocalPos)[1]
- +transform->fR.fkz*(*LocalPos)[2];
- // Unit conversion : Points Units to 3D Units
- (*GlobalPos)[0] /= 288.0;
- (*GlobalPos)[1] /= 288.0;
- (*GlobalPos)[2] /= 288.0;
- }
-
- // Tool to get the Local Coordinates from the Global Coordinates
- void GlobalToLocal(TRANSFORM3D* transform,VECTOR3D* GlobalPos,VECTOR3D* LocalPos) {
- // Origin translation
- (*GlobalPos)[0] -= transform->fT[0];
- (*GlobalPos)[1] -= transform->fT[1];
- (*GlobalPos)[2] -= transform->fT[2];
- // Unit conversion : 3D Units to Points Units
- (*GlobalPos)[0] *= 288.0;
- (*GlobalPos)[1] *= 288.0;
- (*GlobalPos)[2] *= 288.0;
-
- // Axes rotations
- (*LocalPos)[0]=transform->fR.fix*(*GlobalPos)[0]
- +transform->fR.fiy*(*GlobalPos)[1]
- +transform->fR.fiz*(*GlobalPos)[2];
- (*LocalPos)[1]=transform->fR.fjx*(*GlobalPos)[0]
- +transform->fR.fjy*(*GlobalPos)[1]
- +transform->fR.fjz*(*GlobalPos)[2];
- (*LocalPos)[2]=transform->fR.fkx*(*GlobalPos)[0]
- +transform->fR.fky*(*GlobalPos)[1]
- +transform->fR.fkz*(*GlobalPos)[2];
- }
-
- // Tool to get the Local Coordinates from the Global Coordinates for a Vector
- void GlobalToLocalVector(TRANSFORM3D* transform,VECTOR3D* GlobalPos,VECTOR3D* LocalPos) {
- // Unit conversion : 3D Units to Points Units
- (*GlobalPos)[0] *= 288.0;
- (*GlobalPos)[1] *= 288.0;
- (*GlobalPos)[2] *= 288.0;
-
- // Axes rotations
- (*LocalPos)[0]=transform->fR.fix*(*GlobalPos)[0]
- +transform->fR.fiy*(*GlobalPos)[1]
- +transform->fR.fiz*(*GlobalPos)[2];
- (*LocalPos)[1]=transform->fR.fjx*(*GlobalPos)[0]
- +transform->fR.fjy*(*GlobalPos)[1]
- +transform->fR.fjz*(*GlobalPos)[2];
- (*LocalPos)[2]=transform->fR.fkx*(*GlobalPos)[0]
- +transform->fR.fky*(*GlobalPos)[1]
- +transform->fR.fkz*(*GlobalPos)[2];
- }
-
-
-
-
- #undef INTERFACE
- #define INTERFACE ConicCamera
- // Constructor / Destructor of the C++ Object :
- ConicCamera::ConicCamera() {
- fCRef=0; // Reference counter
- // Rotation Matrix initialized to identity :
- fTransform.fR.fix=fTransform.fR.fjy=fTransform.fR.fkz=1.0;
- fTransform.fR.fjx=fTransform.fR.fkx=0.0;
- fTransform.fR.fiy=fTransform.fR.fky=0.0;
- fTransform.fR.fiz=fTransform.fR.fjz=0.0;
- // Translation Vector initialized to zero :
- fTransform.fT[0]=fTransform.fT[1]=fTransform.fT[2]=0.0;
- // Initial Zoom Coefficient
- fData.fZoomCoef=10;
- fQuickZoom =((NUM3D)fData.fZoomCoef)/10.0; // fZoomCoef = 10x Zoom
- fCoef=kQuick288*kQuickmmTo3DUnit; // Convert mm in Point Unit
- }
-
- ConicCamera::~ConicCamera() {
- global_count_Obj--;
- }
-
- // IUnknown Interface :
- HRESULT ConicCamera::QueryInterface(THIS_ REFIID riid,LPVOID FAR* ppvObj) {
- *ppvObj=NULL;
-
- // The ConicCamera knows the interfaces of the parent Objects
- if (IsEqualIID(riid, IID_IUnknown))
- *ppvObj=(LPVOID)this;
- else if (IsEqualIID(riid, IID_I3DExCamera))
- *ppvObj=(LPVOID)this;
- else if (IsEqualIID(riid, IID_I3DExDataExchanger))
- *ppvObj=(LPVOID)this;
- else if (IsEqualIID(riid, IID_I3DExtension))
- *ppvObj=(LPVOID)this;
-
- // we must add reference if we return an interface
- if (*ppvObj!=NULL) {
- ((LPUNKNOWN)*ppvObj)->AddRef();
- return NOERROR;
- }
- else {
- return ResultFromScode(E_NOINTERFACE);
- }
- }
-
- ULONG ConicCamera::AddRef(THIS) {
- return fCRef++;
- }
-
- ULONG ConicCamera::Release(THIS) {
- ULONG UnreleaseObject=fCRef--;
-
- if (fCRef==0)
- delete this; // No reference left so delete the object
-
- return UnreleaseObject;
- // Use local variable because if the object is destructed
- // fCRef do not exist
- }
-
- // I3DExtension methods :
- I3DExtension* ConicCamera::Clone(THIS) {
- ConicCamera* theClone = new ConicCamera;
- if (theClone) {
- theClone->AddRef();
- theClone->fTransform=fTransform; // Copy all the Data
- theClone->fData =fData;
- theClone->fQuickZoom=fQuickZoom;
- theClone->fCoef =fCoef;
- }
- return theClone;
- }
-
- HRESULT ConicCamera::ShellUtilitiesInit(THIS_ IShUtilities* shellUtilities) {
- InitCoFailure(shellUtilities);
- return NOERROR;
- }
-
- // I3DExDataExchanger methods :
- ExtensionDataMap* ConicCamera::GetExtensionDataMap(THIS) {
- return NULL;
- }
-
- void* ConicCamera::GetExtensionDataBuffer(THIS) {
- return &fData; // The Shell uses this pointer to set the values of the camera's parameters
- }
-
- HRESULT ConicCamera::ExtensionDataChanged(THIS) {
- fQuickZoom=((NUM3D)fData.fZoomCoef)/10.0;
- // the Zoom have changed, so recalculate the Zoom factor.
- return NOERROR;
- }
-
- // This function is here, to allow you to create and use special resource.
- HRESULT ConicCamera::HandleEvent(THIS_ ULONG SourceID) {
- return ResultFromScode(E_NOTIMPL);
- }
-
- short ConicCamera::GetResID(THIS) {
- return 131; // This is the view ID in the resource file
- }
-
- // I3DExCamera methods :
- // set the coordinates Transformation values
- HRESULT ConicCamera::SetTransform(THIS_ TRANSFORM3D* transform) {
- fTransform=*transform; // Copy the data of transform and not the pointer.
- return NOERROR;
- }
-
- // Ray Creation :
- BOOLEAN ConicCamera::CreateRay(THIS_ VECTOR2D* screenPosition, VECTOR3D* resultOrigin, VECTOR3D* resultDirection) {
- VECTOR3D tempV;
-
- // Origin of the Ray is on the Screen in the Global Coordinates System
- tempV[0]=(*screenPosition)[0]/fCoef/fQuickZoom;
- tempV[1]=(*screenPosition)[1]/fCoef/fQuickZoom;
- tempV[2]=0.0;
- LocalToGlobal(&fTransform,&tempV,resultOrigin);
-
- // Create the vector from the Center of projection to the screenPoint
- tempV[2]=-kQuickFocal;
-
- LocalToGlobalVector(&fTransform,&tempV,resultDirection);
-
- NUM3D norm=sqrt(SQR((*resultDirection)[0]) + SQR((*resultDirection)[1]) + SQR((*resultDirection)[2]));
- (*resultDirection)[0] /= norm;
- (*resultDirection)[1] /= norm;
- (*resultDirection)[2] /= norm; // the Direction vector must be normalized
-
- return TRUE;
- }
-
-
- // 3D Projection :
- BOOLEAN ConicCamera::Project3DTo2D(THIS_ VECTOR3D* position, VECTOR2D* resultScreenPosition, NUM3D* resultDistanceToScreen) {
- NUM3D temp;
-
- *resultDistanceToScreen=-(*position)[2]; // Distance to the screen (-z) in Point Unit
-
- temp=kQuickFocal*kQuick288*kQuickmmTo3DUnit; // Focal length in Point Unit
- if ((*position)[2]!=temp) {
- temp-=(*position)[2]; // Distance from the 3D Point to the Focal Point
- (*resultScreenPosition)[0]=(*position)[0]*kQuickFocal/temp*fQuickZoom;
- (*resultScreenPosition)[1]=(*position)[1]*kQuickFocal/temp*fQuickZoom;
- // Conical projection
- }
- else {
- (*resultScreenPosition)[0]=0.0;
- (*resultScreenPosition)[1]=0.0;
- // the Point is in the Focal Plane so it can't be projected on the screen
- }
-
- (*resultScreenPosition)[0]*=fCoef*fCoef;
- (*resultScreenPosition)[1]*=fCoef*fCoef; // Conversion in Point Unit
-
- if (*resultDistanceToScreen<=0.0)
- return FALSE; // The point is behind the Camera
- else
- return TRUE; // The point is in front of the Camera, it's visible.
- }
-
- ULONG ConicCamera::GetPrimitiveID(THIS) {
- return 0;
- }
-
- ULONG ConicCamera::Clip3D(THIS_ FACET3D* localFacet, VERTEX3D* localVertices,
- FACET3D* cameraFacet, VERTEX3D* cameraVertices,
- NUM3D* clipBox) {
- return 0; /*** to do ***/
- }
-
-
- BOOLEAN ConicCamera::ClipLine3D(THIS_ VECTOR3D* P1, VECTOR3D* P2) {
- return FALSE; /*** to do ***/
- }
-